<template>
	<transition name="fade">
		<div v-if="visible" class="console-panel">
			<div class="console-header">
				<span>控制台</span>
				<el-button size="small" icon="el-icon-close" @click="$emit('close')" circle plain />
			</div>
			<div class="console-body">
				<pre class="console-log">{{ logs }}</pre>
			</div>
		</div>
	</transition>
</template>
<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
const props = defineProps({ visible: Boolean })
const logs = ref('加载中...')
let timer = null
const fetchLogs = async () => {
	try {
		const res = await fetch('/api/logs')
		logs.value = await res.text()
	} catch {
		logs.value = '无法获取日志信息'
	}
}
onMounted(() => {
	if (props.visible) {
		fetchLogs()
		timer = setInterval(fetchLogs, 3000)
	}
})
watch(() => props.visible, v => {
	if (v) {
		fetchLogs()
		timer = setInterval(fetchLogs, 3000)
	} else if (timer) {
		clearInterval(timer)
		timer = null
	}
})
onUnmounted(() => { if (timer) clearInterval(timer) })
</script>
<style scoped>
.console-panel {
	position: fixed;
	right: 32px;
	bottom: 32px;
	width: 420px;
	max-height: 340px;
	background: #fff6fa;
	border-radius: 18px;
	box-shadow: 0 4px 24px #ffd6e6;
	z-index: 9999;
	display: flex;
	flex-direction: column;
	border: 1.5px solid #ffb6e6;
	overflow: hidden;
}
.console-header {
	background: linear-gradient(90deg, #ffb6e6 0%, #b2f7ef 100%);
	color: #fff;
	font-weight: bold;
	padding: 10px 18px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	font-size: 1.1rem;
}
.console-body {
	padding: 12px 18px;
	overflow-y: auto;
	font-size: 0.98rem;
	color: #7a5e8c;
	background: #fff;
	flex: 1;
}
.console-log {
	white-space: pre-wrap;
	word-break: break-all;
	margin: 0;
	font-family: 'JetBrains Mono', 'Consolas', 'monospace';
}
.fade-enter-active, .fade-leave-active {
	transition: opacity 0.2s;
}
.fade-enter-from, .fade-leave-to {
	opacity: 0;
}
</style>

